package etc.com.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import Controls.DialogHelper;
import etc.com.market.R;
import etc.com.market.MarketApp;

public class ConnectionManager {
	private static Connection con = null;
	private String userid="scott", password = "tiger";
	private String url = "jdbc:odbc:bob";
	private static Statement m_sttmt;
	public ConnectionManager() {
		
	}
	
	public static Connection GetConnection() throws ClassNotFoundException, SQLException{
		
		if( con == null){
			con = CreateConnection();			
		}
		return con;
	}
	
	
	public static void setConnection(Connection con) {
		ConnectionManager.con = con;
	}

	public static Statement GetStatement()  {
		
		try {
			if( con == null || con.isClosed()){
				con = CreateConnection();	
			}
			m_sttmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
		} catch (SQLException e) {
			ShowErrorMessage();
		}
		
		return m_sttmt;
	}
	
	
	private static Connection CreateConnection() {
		Connection DBConnSQL = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			DBConnSQL = DriverManager.getConnection
			
			 ("jdbc:mysql://localhost/suppermarket?autoReconnect=true",
					 "root","root");
		} catch (ClassNotFoundException e) {
			ShowErrorMessage();
		} catch (SQLException e) {
			ShowErrorMessage();
		}
		
		 return DBConnSQL;
	}
	
	
	private static void ShowErrorMessage(){
		DialogHelper.ShowMessage(MarketApp.seft, MarketApp.seft.getText(R.string.msg_lose_connect));
		//Church.seft.CloseApp();
	}
	
}
